local L = AceLibrary("AceLocale-2.2"):new("LeaveMeAlone")

local options = {
	type='group',
	args = {
		showMsg = {
			type = 'text',
			order = 5,
			name = L["Show Message"],
			desc = L["When a request is blocked, where do you want the message to show?"],
			get = function()
				return LeaveMeAlone.db.profile.showMsg
			end,
			set = function(v)
				LeaveMeAlone.db.profile.showMsg = v
			end,
			validate = {["NONE"] = L["None"], ["CHAT"] = L["Chat Frame"], ["SCREEN"] = L["Screen"], ["BOTH"] = L["Both"]},
		},
		numBlocks = {
			type = 'range',
			order = 6,
			min = 0,
			max = 5,
			step = 1,
			name = L["Offenses"],
			desc = L["Players who bug you more than this many times will be ignored (0 = Do not ignore)"],
			get = function()
				return LeaveMeAlone.db.profile.numBlocks
			end,
			set = function(v)
				LeaveMeAlone.db.profile.numBlocks = v
			end,
		},
		resetOptions = {
			type = 'execute',
			order = 7,
			name = L["Default Options"],
			desc = L["Resets your settings to the Default "],
			func = function()
				LeaveMeAlone.db.profile.ignoreDuels = true
				LeaveMeAlone.db.profile.duelReply = true
				LeaveMeAlone.db.profile.duelReplyMsg = L["I do not wish to Duel at this time."]
				LeaveMeAlone.db.profile.showMsg = "CHAT"
				LeaveMeAlone.db.profile.ignoreParty = true
				LeaveMeAlone.db.profile.partyReply = true
				LeaveMeAlone.db.profile.partyReplyMsg = L["I do not wish to Group with you at this time."]
				LeaveMeAlone.db.profile.ignoreGuild = true
				LeaveMeAlone.db.profile.guildReply = true
				LeaveMeAlone.db.profile.guildReplyMsg = L["I do not wish to join your Guild at this time."]
				LeaveMeAlone.db.profile.numBlocks = 3
			end,
		},
		clearBlockCount = {
			type = 'execute',
			order = 8,
			name = L["Clear Offenses"],
			desc = L["Clears the list of Offenses"],
			func = function()
				LeaveMeAlone.db.char.blocks = { ['*'] = 0 }
			end,
		},
		duelSettings = {
			type = 'group',
			name = L["Duel Options"],
			desc = L["Set options pertaining to Duels"],
			args = {
				ignoreDuels = {
					type = 'toggle',
					order = 2,
					name = L["Ignore Duels"],
					desc = L["Toggles whether or not Duel Requests are ignored"],
					get = function()
						return LeaveMeAlone.db.profile.ignoreDuels
					end,
					set = function()
						LeaveMeAlone.db.profile.ignoreDuels = not LeaveMeAlone.db.profile.ignoreDuels
					end,
					map = { [false] = L["You are no longer blocking Duel Requests."], [true] = L["You are now blocking Duel Requests."] },
				},
				duelReply = {
					type = 'toggle',
					order = 3,
					name = L["Reply to Blocked Duels"],
					desc = L["Toggles whether or not you '/w' the player who challenged you"],
					get = function()
						return LeaveMeAlone.db.profile.duelReply
					end,
					set = function()
						LeaveMeAlone.db.profile.duelReply = not LeaveMeAlone.db.profile.duelReply
					end,
					map = { [false] = L["You are no longer replying to Duel Requests."], [true] = L["You are now replying to Duel Requests."] },
				},
				duelReplyMsg = {
					type = 'text',
					order = 4,
					name = L["Duel Blocked Message"],
					desc = L["This is the message you will send to the player who challenged you to a Duel."],
					usage = L["<Your Message Here>"],
					get = function()
						return LeaveMeAlone.db.profile.duelReplyMsg
					end,
					set = function(v)
						LeaveMeAlone.db.profile.duelReplyMsg = v
					end,
					disabled = function() return not LeaveMeAlone.db.profile.duelReply end,
				},
			},
		},
		partySettings = {
			type = 'group',
			name = L["Party Invite Options"],
			desc = L["Set options pertaining to Party Invitations"],
			args = {
				ignoreParty = {
					type = 'toggle',
					order = 2,
					name = L["Ignore Party Invites"],
					desc = L["Toggles whether or not Party Invites are ignored"],
					get = function()
						return LeaveMeAlone.db.profile.ignoreParty
					end,
					set = function()
						LeaveMeAlone.db.profile.ignoreParty = not LeaveMeAlone.db.profile.ignoreParty
					end,
					map = { [false] = L["You are no longer blocking Party Invites."], [true] = L["You are now blocking Party Invites."] },
				},
				partyReply = {
					type = 'toggle',
					order = 3,
					name = L["Reply to Blocked Party Invites"],
					desc = L["Toggles whether or not you '/w' the player who invited you"],
					get = function()
						return LeaveMeAlone.db.profile.partyReply
					end,
					set = function()
						LeaveMeAlone.db.profile.partyReply = not LeaveMeAlone.db.profile.partyReply
					end,
					map = { [false] = L["You are no longer replying to Party Invites."], [true] = L["You are now replying to  Party Invites."] },
				},
				partyReplyMsg = {
					type = 'text',
					order = 4,
					name = L["Party Invite Blocked Message"],
					desc = L["This is the message you will send to the player who invited you to a Party."],
					usage = L["<Your Message Here>"],
					get = function()
						return LeaveMeAlone.db.profile.partyReplyMsg
					end,
					set = function(v)
						LeaveMeAlone.db.profile.partyReplyMsg = v
					end,
					disabled = function()
						return not LeaveMeAlone.db.profile.partyReply
					end,
				},
			},
		},
		guildSettings = {
			type = 'group',
			name = L["Guild Invite Options"],
			desc = L["Set options pertaining to Guild Invitations"],
			args = {
				ignoreGuild = {
					type = 'toggle',
					order = 2,
					name = L["Ignore Guild Invites"],
					desc = L["Toggles whether or not Guild Invitations are ignored"],
					get = function()
						return LeaveMeAlone.db.profile.ignoreGuild
					end,
					set = function()
						LeaveMeAlone.db.profile.ignoreGuild = not LeaveMeAlone.db.profile.ignoreGuild
					end,
					map = { [false] = L["You are no longer blocking Guild Invites."], [true] = L["You are now blocking Guild Invites."] },
				},
				guildReply = {
					type = 'toggle',
					order = 3,
					name = L["Reply to Blocked Guild Invitess"],
					desc = L["Toggles whether or not you '/w' the player who Invited you to a Guild"],
					get = function()
						return LeaveMeAlone.db.profile.guildReply
					end,
					set = function()
						LeaveMeAlone.db.profile.guildReply = not LeaveMeAlone.db.profile.guildReply
					end,
					map = { [false] = L["You are no longer replying to Guild Invites."], [true] = L["You are now replying to Guild Invites."] },
				},
				guildReplyMsg = {
					type = 'text',
					order = 4,
					name = L["Guild Invite Blocked Message"],
					desc = L["This is the message you will send to the player who Invited you to a Guild."],
					usage = L["<Your Message Here>"],
					get = function()
						return LeaveMeAlone.db.profile.guildReplyMsg
					end,
					set = function(v)
						LeaveMeAlone.db.profile.guildReplyMsg = v
					end,
					disabled = function() return not LeaveMeAlone.db.profile.guildReply end,
				},
			},
		},
	},
}

LeaveMeAlone = AceLibrary("AceAddon-2.0"):new("AceConsole-2.0", "AceEvent-2.0", "AceDB-2.0")
LeaveMeAlone:RegisterDB("LeaveMeAloneDB", "LeaveMeAloneDBPC");
LeaveMeAlone:RegisterDefaults("profile", {
	ignoreDuels = true,
	duelReply = true,
	duelReplyMsg = L["I do not wish to Duel at this time."],
	showMsg = "CHAT",
	ignoreParty = true,
	partyReply = true,
	partyReplyMsg = L["I do not wish to Group with you at this time."],
	ignoreGuild = true,
	guildReply = true,
	guildReplyMsg = L["I do not wish to join your Guild at this time."],
	numBlocks = 3,
} )
LeaveMeAlone:RegisterDefaults("char", {
	blocks = {
		['*'] = 0
	}
	
})
LeaveMeAlone:RegisterChatCommand(L["Slash-Commands"], options)

function LeaveMeAlone:OnEnable()
    self:RegisterEvent("DUEL_REQUESTED")
    self:RegisterEvent("PARTY_INVITE_REQUEST")
    self:RegisterEvent("GUILD_INVITE_REQUEST")
end

function LeaveMeAlone:Blocked(player)
	if self.db.profile.numBlocks > 0 then
		if self.db.char.blocks[player] == nil then
			self.db.char.blocks[player] = 1
		else
			self.db.char.blocks[player] = self.db.char.blocks[player] + 1
		end
		if self.db.char.blocks[player] >= self.db.profile.numBlocks then
			AddIgnore(player)
		end
	end
	if self.db.char.blocks['total'] == nil then
		self.db.char.blocks['total'] = 1 
	else
		self.db.char.blocks['total'] = self.db.char.blocks['total'] + 1
	end
end

function LeaveMeAlone:DisplayMessage(msg)
	if self.db.profile.showMsg == "CHAT" or self.db.profile.showMsg == "BOTH" then
		self:Print(msg)
	end
	if self.db.profile.showMsg == "SCREEN" or self.db.profile.showMsg == "BOTH" then
		UIErrorsFrame:AddMessage(msg, 1.0, 1.0, 1.0, 5.0)
	end
end

function LeaveMeAlone:DUEL_REQUESTED(arg1)
	if self.db.profile.ignoreDuels then
		self:DisplayMessage(L["Duel incoming from "] .. arg1 .. L[", canceling."])
		if self.db.profile.duelReply then
			SendChatMessage(self.db.profile.duelReplyMsg, "WHISPER", nil, arg1)
		end
		CancelDuel()
		self:Blocked(arg1)
	else
		self:DisplayMessage(L["Duel incoming from "] .. arg1)
	end
end

function LeaveMeAlone:PARTY_INVITE_REQUEST(arg1)
	if self.db.profile.ignoreParty then
		self:DisplayMessage(L["Party Invite incoming from "] .. arg1 .. L[", canceling."])
		if self.db.profile.partyReply then
			SendChatMessage(self.db.profile.partyReplyMsg, "WHISPER", nil, arg1)
		end
		DeclineGroup()
		StaticPopup_Hide("PARTY_INVITE")
		self:Blocked(arg1)
		
	else
		self:DisplayMessage(L["Party Invite incoming from "] .. arg1)
	end
end

function LeaveMeAlone:GUILD_INVITE_REQUEST(arg1, arg2)
	if self.db.profile.ignoreGuild then
		self:DisplayMessage(L["You have recieved a Guild Invitaion from "] .. arg1 .. L[" for the guild "] .. arg2 .. L[", canceling."])
		if self.db.profile.guildReply then
			SendChatMessage(self.db.profile.guildReplyMsg, "WHISPER", nil, arg1)
		end
		DeclineGroup()
		StaticPopup_Hide("GUILD_INVITE")
		self:Blocked(arg1)
	else
		self:DisplayMessage(L["You have recieved a Guild Invitaion from "] .. arg1 .. L[" for the guild "] .. arg2)
	end
end
